# emailalert-lib.pl
# functions used by emailalert

do '../web-lib.pl';
&init_config();


# read config data from file
sub get_conf_data
{
#&lock_file($config{'file_conf'});
#my $file = &read_file_lines($config{'file_conf'});
#&unlock_file($config{'file_conf'});
open(ARFILE, "$config{'file_conf'}") || 
(&nas_syslog("emailalert","naslog_warning",18000,"the emailalert configuration file can't be opened!") 
&& die "couldn't open file: $config{'file_conf'}");

my %configData;

while(<ARFILE>)
#for(my $i=0;$i<@$file;$i++)
{
	next if(/^#/);
	m/(\w+)[ \t]*=[ \t]*(.*)/;
	$configData{$1} = $2;
}
return %configData;
}

# save_conf_data(%{status},{level},{receiver},{smtp},{crit},{info},{warning})
# save config data to file
sub save_conf_data
{
my($d) = $_[0];

#&lock_file($config{'file_conf'});
my $file = &read_file_lines($config{'file_conf'});
for(my $i=0;$i<@$file;$i++)
{
	next if($file->[$i] =~ /^#/);
	$file->[$i] =~ s/^status.*/status = $d->{'status'}/;
        $file->[$i] =~ s/^hqstat.*/hqstat = $d->{'hqstat'}/;
	$file->[$i] =~ s/^passwd.*/passwd = $d->{'passwd'}/;
	$file->[$i] =~ s/^sender.*/sender = $d->{'sender'}/;
	$file->[$i] =~ s/^receiver.*/receiver = $d->{'receiver'}/;
	$file->[$i] =~ s/^smtp.*/smtp = $d->{'smtp'}/;
	$file->[$i] =~ s/^crit.*/crit = $d->{'crit'}/;
	$file->[$i] =~ s/^info.*/info = $d->{'info'}/;
	$file->[$i] =~ s/^warning.*/warning = $d->{'warning'}/;
}
&flush_file_lines();
#&unlock_file($config{'file_conf'});
}
sub mdm_email
{
	save_conf_data($_[0]);
	return "Success";
}
